<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use App\Models\UserAddress;
use Illuminate\Http\Request;
use App\Common\ErrorCode;

class UserAddressesController extends BaseController
{
    //分页获取收货地址
    public function index(Request $request)
    {
        $page = intval($request->input('page',1));
        $per_page = intval($request->input('per_page',20));
        $user_id = intval($request->input('user_id'));

        $addresses = UserAddress::where('user_id',$user_id)->orderBy('last_used_at','desc')->paginate($per_page);

        $data = [
            '_meta' => [
                'total_count' => $addresses->total(),
                'page_count' => $addresses->lastPage(),
                'current_page' => $addresses->currentPage(),
                'per_page' => $addresses->perPage()
            ],
            "items" => $addresses->items()
        ];
        return $this->res([
            'data' => $data
        ]);
    }

    //添加收货地址
    public function store(Request $request)
    {
        $data['contact_telephone'] = $request->input('contact_telephone');
        $data['user_id'] = $request->input('user_id');
        $data['address'] = $request->input('address');
        $data['contact_name'] = $request->input('contact_name');
        $data['province'] = $request->input('province');
        $data['city'] = $request->input('city');
        $data['area'] = $request->input('area');

        if(empty($data['contact_telephone']) || empty($data['province']) || empty($data['city']) || empty($data['area']) || empty($data['user_id']) || empty($data['address']) || empty($data['contact_name']))
        {
            return $this->error(ErrorCode::PARAMS_NOT_VALID_CODE,ErrorCode::PARAMS_NOT_VALID_MESSAGE);
        }

        if(!isMobile($data['contact_telephone']))
        {
            return $this->error(ErrorCode::TELEPHONE_NOT_VALID_CODE,ErrorCode::TELEPHONE_NOT_VALID_MESSAGE);
        }

        $user = User::find($data['user_id']);
        if(empty($user))
        {
            return $this->error(ErrorCode::USER_NOT_VALID_CODE,ErrorCode::USER_NOT_VALID_MESSAGE);
        }

        $address = UserAddress::create($data);
        return $this->success();
    }

    //修改收货地址
    public function update(UserAddress $user_address, Request $request)
    {
        $data['contact_telephone'] = $request->input('contact_telephone');
        $data['user_id'] = $request->input('user_id');
        $data['address'] = $request->input('address');
        $data['contact_name'] = $request->input('contact_name');
        $data['province'] = $request->input('province');
        $data['city'] = $request->input('city');
        $data['area'] = $request->input('area');

        if(empty($data['contact_telephone']) || empty($data['province']) || empty($data['city']) || empty($data['area']) || empty($data['user_id']) || empty($data['address']) || empty($data['contact_name']))
        {
            return $this->error(ErrorCode::PARAMS_NOT_VALID_CODE,ErrorCode::PARAMS_NOT_VALID_MESSAGE);
        }

        if(!isMobile($data['contact_telephone']))
        {
            return $this->error(ErrorCode::TELEPHONE_NOT_VALID_CODE,ErrorCode::TELEPHONE_NOT_VALID_MESSAGE);
        }

        if($data['user_id'] == $user_address->user_id)
        {
            $user_address->update($data);
            return $this->success();
        }

        return $this->error(ErrorCode::UPDATE_ERROR_CODE,ErrorCode::UPDATE_ERROR_MESSAGE);
    }

    //查看收货地址
    public function show(UserAddress $user_address)
    {
        return $this->res([
            'data' => [
                'id' => $user_address->id,
                'contact_telephone' => $user_address->contact_telephone,
                'contact_name' => $user_address->contact_name,
                'province' => $user_address->province,
                'city' => $user_address->city,
                'area' => $user_address->area,
                'address' => $user_address->address,
            ]
        ]);
    }

    public function destroy(UserAddress $user_address, Request $request)
    {
        $data['user_id'] = $request->input('user_id');

        if(empty($data['user_id']))
        {
            return $this->error(ErrorCode::PARAMS_NOT_VALID_CODE,ErrorCode::PARAMS_NOT_VALID_MESSAGE);
        }

        if($user_address->user_id == $data['user_id'])
        {
            $user_address->delete();
            return $this->success();
        }
        return $this->error(ErrorCode::DELETE_ERROR_CODE,ErrorCode::DELETE_ERROR_MESSAGE);
    }
}
